package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {
    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;


    /**
     * 新增
     *
     * @param menu
     * @return
     */
    @RequestMapping("/menuAdd")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result menuAdd(@RequestBody Menu menu) {
        log.info("[菜单-新增]data:{}", menu);
        menuService.menuAdd(menu);
        return new Result(true, "新增菜单成功", menu);
    }

    /**
     * 查询
     *
     * @param
     * @return
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findAll(QueryPageBean queryPageBean) {
        log.info("[菜单-查询]data:{}", queryPageBean);
        PageResult pageResult = menuService.findAll(queryPageBean);
        return new Result(true, "分页查询成功", pageResult);
    }

    /**
     * 回显菜单
     *
     * @param
     * @return
     */
    @RequestMapping("/menuUpdate")
    public Result menuUpdate(Integer id) {
        log.info("[菜单-回显]id:{}", id);
        Menu menu = menuService.menuUpdate(id);
        return new Result(true, "回显菜单成功", menu);
    }

    /**
     * 编辑菜单
     *
     * @param
     * @return
     */
    @RequestMapping("/menuEdit")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result menuEdit(@RequestBody Menu menu) {
        log.info("[菜单-编辑]data:{}", menu);
        menuService.menuEdit(menu);
        return new Result(true, "编辑菜单成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/menuDelete")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result menuDelete(Integer id) {
        log.info("[菜单-删除]id:{}", id);

        menuService.menuDelete(id);

        return new Result(true, "删除菜单成功");
    }

    /**
     * 动态获取菜单
     * @return
     */
//    @RequestMapping("getMenu/{name}")
//    public Result gerMenus(@PathVariable String name) {
//        log.info(":::{}++++++++++++++++++++++++++++++++++", name);
//        return userService.getMenu(name);
//    }

}
